package com.onetech.adrewards.free.dialogs;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.adapters.ListExchangeAdapter;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.fragments.ExchangeFragment.ExchangeHandler;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.models.GiftCardModel;
import com.onetech.adrewards.free.objects.ExchangeGiftCarInfoDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;
import com.zuzu.dialogs.BaseDialog;

public class ExchangeSelectDialog extends BaseDialog implements OnItemClickListener{
	private ListView mListView = null;
	private LoadExchangeGiftCardInfo mLoadExchangeGiftCardInfo = null;
	private ListExchangeAdapter mAdapter=null;
	private Activity activity=null;
	private ProgressWheel mProgressBar=null;
	private View mExchangerContainer=null;
	private TextView mExchangeTxt=null;
	private int typeExchange=0;
	private ExchangeHandler mExchangehandler=null;
	private Tracker mTracker;
	
	public ExchangeSelectDialog(Activity context,ExchangeHandler _handler) {
		super(context, context.getString(R.string.exchange_select_title),
				TYPE_1_BUTTON, R.layout.exchange_giftcard_select_type_layout);
		activity=context;
		mExchangehandler=_handler;
		
		mExchangerContainer=(LinearLayout)findViewById(R.id.exchangeContainer);
		mExchangeTxt=(TextView)findViewById(R.id.exchangeTxt);
		mListView = (ListView) findViewById(R.id.listExchange);
		mAdapter=new ListExchangeAdapter(activity);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		mProgressBar=(ProgressWheel)findViewById(R.id.exchangeProgressBar);
		mProgressBar.spin();
		
		showHideProgressBar(true);
		setPositiveButtonTitle(R.string.dialog_exchange_select_Ok);
		setNegativeButtonTitle(R.string.dialog_exchange_select_Cancel);
		setDefaultButtonTitle(R.string.dialog_exchange_select_Cancel);
		setOnDialogListener(new OnDialogListener() {

			@Override
			public void onPositiveButtonClicked() {
				dismiss();
			}

			@Override
			public void onNegativeButtonClicked() {
				dismiss();
			}

			@Override
			public void onDefaultButtonClicked() {
				dismiss();
			}
		});
		loadExchangeGiftCardInfo();
	}
	private void showHideProgressBar(boolean show){
		if(show){
			mProgressBar.setVisibility(View.VISIBLE);
			mExchangerContainer.setVisibility(View.GONE);
		}
		else{
			mProgressBar.setVisibility(View.GONE);
			mExchangerContainer.setVisibility(View.VISIBLE);
		}
	}
	private void loadExchangeGiftCardInfo() {
		if (mLoadExchangeGiftCardInfo == null
				|| mLoadExchangeGiftCardInfo.getStatus() == Status.FINISHED) {
			mLoadExchangeGiftCardInfo = new LoadExchangeGiftCardInfo();
			mLoadExchangeGiftCardInfo.execute();
		}
	}
	
	private class LoadExchangeGiftCardInfo extends
			AsyncTask<Void, Void, ExchangeGiftCarInfoDTO> {

		@Override
		protected ExchangeGiftCarInfoDTO doInBackground(Void... params) {
			return GiftCardModel.getInstance().getExchangeGiftCardInfo();
		}

		@Override
		protected void onPostExecute(ExchangeGiftCarInfoDTO result) {
			super.onPostExecute(result);
			try {
				if (result != null) {
					int userCoins=result.getTotal_coin();
					CommonModel.getInstance().setStore(Cache.ONE_TECH_USER_POINTS, String.valueOf(userCoins));
					
					if (userCoins >= AppConfig.MIN_COIN_EXCHANGE && result.getCards().size()>0) {
						String strExchange=String.valueOf(userCoins)+"->"+String.valueOf(userCoins-AppConfig.MIN_COIN_EXCHANGE)+"pt";
						mExchangeTxt.setText(strExchange);
						mAdapter.updateAppList(result.getCards());
						showHideProgressBar(false);
					} else {
						AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.error_message_not_enough_point));
						mExchangehandler.reLoadFragment(true,true,true);						
						dismiss();
					}
				}else{
					AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.error_message_not_enough_point));
					dismiss();
				}
			} catch (Exception e) {
				AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.exchange_failed_error_message));
				mExchangehandler.reLoadFragment(true,true,true);
				dismiss();
			}
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		typeExchange=position+1;
		//mAdapter.updateChecked(mAdapter.getItem(position).getCard_type());
		exchangeRightNow();		
	}
	private void exchangeRightNow(){
		if(typeExchange<1){
			return;
		}
		AdrewadsUtils.checkUId();
		if(AppConfig.uId!=null && AppConfig.uId.length()>0){
			ExchangeProcessDialog mExchangeProcessDialog=new ExchangeProcessDialog(activity, AppConfig.uId, typeExchange,mExchangehandler);
			mExchangeProcessDialog.show();
		}else{
			AdrewadsUtils.showMessage(getContext(), getContext().getString(R.string.exchange_failed_error_message));					
		}
		dismiss();
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, activity.getString(R.string.ExchangeSelectDialog));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
